﻿@namespace Masa.Blazor.Presets
@typeparam TValue
@inherits PDateTimePickerView<TValue>

<CascadingValue Value="this" IsFixed>
    @if (IsDialog)
    {
        <MDialog Value="_menu"
                 ValueChanged="OnMenuChanged"
                 ActivatorContent="@ComputedActivatorContent"
                 ContentClass="@Class">
            <ChildContent>
                @ViewContent()
                @ActionsContent()
            </ChildContent>
        </MDialog>
    }
    else
    {
        <MMenu Value="_menu"
               ValueChanged="OnMenuChanged"
               CloseOnContentClick="false"
               ToggleOnClick="false"
               OffsetY
               NudgeBottom="1"
               ActivatorContent="@ComputedActivatorContent"
               ContentClass="@Class">
            <ChildContent>
                @ViewContent()
                @ActionsContent()
            </ChildContent>
        </MMenu>
    }

    @ChildContent
</CascadingValue>

@code {

    protected virtual RenderFragment CompactPickerView() => __builder => { };
    protected virtual RenderFragment DefaultPickerView() => __builder => { };

    private RenderFragment<ActivatorProps> ComputedActivatorContent => ActivatorContent ?? DefaultActivator;

    private RenderFragment DefaultActivator(ActivatorProps context) => __builder =>
    {
        _defaultActivator ??= new();

        <MTextField Value="_display"
                    ValueChanged="DisplayChanged"
                    UpdateOnChange
                    TValue="string?"
                    Readonly="@Readonly"
                    Disabled="@Disabled"
                    Clearable="@(!Readonly && _defaultActivator.Clearable)"
                    Dense="@_defaultActivator.Dense"
                    Label="@_defaultActivator.Label"
                    Filled="@_defaultActivator.Filled"
                    HideDetails="@_defaultActivator.HideDetails"
                    Outlined="@_defaultActivator.Outlined"
                    Solo="@_defaultActivator.Solo"
                    SoloInverted="@_defaultActivator.SoloInverted"
                    RootAttributes="@context.Attrs">
        </MTextField>
    };

    private RenderFragment ViewContent() => __builder =>
    {
        @if (IsCompact)
        {
            @CompactPickerView()
        }
        else
        {
            @DefaultPickerView()
        }
    };

    private RenderFragment ActionsContent() => __builder =>
    {
        <MDivider/>
        <div class="@BasePickerBlock.Element("actions")">
            <div>
                <MDefaultsProvider Defaults="@_shortcutDefaults">
                    <MButton Text
                             Small
                             Color="primary"
                             OnClick="@(() => _shortcutContext.Update(DateTime.Now))">
                        @I18n.T("$masaBlazor.datePicker.today")
                    </MButton>
                    @ShortcutsContent?.Invoke(_shortcutContext)
                </MDefaultsProvider>
            </div>
            <MSpacer/>
            <MButton Color="primary"
                     OnClick="@HandleOnConfirm"
                     Dark="@Dark"
                     Light="@Light">
                @I18n.T("$masaBlazor.ok")
            </MButton>
        </div>
    };

}